package top.suven.base.util.utilize;


import org.apache.commons.codec.digest.DigestUtils;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class DigestUtil {
	
	public static final String md5Key = "ku&Gou*7";	
	//CPA广告md5Key
	public static final String CPA_Ad_md5Key = "$#cpa_ad_info*?";
	 public static String md5(String password,String key) {		 
		 return DigestUtils.md5Hex(password+key);
	 }
	 

	 public static String md5(String password) {
			return md5(password,md5Key);
	 }    
	 
	 public static String md5(byte [] data) {
		 return md5(data,md5Key);
	 }
	 
	 /**
	  * md5 返回 32 号小写
	  * @param data
	  * @return
	  */
	 public static String md5(byte [] data,String key) {	
		byte [] keys = key.getBytes();		    
	    ByteBuffer buffer =  ByteBuffer.allocate(data.length+keys.length);		    
	    buffer.put(data);
	    buffer.put(keys);
	    buffer.flip();				
	    return DigestUtils.md5Hex(buffer.array());
	 } 
	 
	 /**
	  * 生成length长度的随机字符串(包含数字、大小写字母)
	  * @param length
	  * @return
	  */
	 public static String getRandomMd5(int length) {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; ++i) {
			int number = random.nextInt(3);
			long result = 0;

			switch (number) {
			case 0:
				result = Math.round(Math.random() * 25 + 65);
				sb.append(String.valueOf((char) result));
				break;
			case 1:
				result = Math.round(Math.random() * 25 + 97);
				sb.append(String.valueOf((char) result));
				break;
			case 2:
				sb.append(String.valueOf(new Random().nextInt(10)));
				break;
			}
		}
		return sb.toString();
	}
	 
    /**
     * 生成的验证码与随机数的对应关系的map
     * randomMd5参数
     * 以及randomMd5+干扰字符串进行md5加密后截取16位的字符串(私钥)
     * 产生的验证码和一位随机数的对应关系
     * @param randomMd5
     * @return
     * @throws Exception
     */
	public static Map<String,Integer> getMd5Pass(String randomMd5) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		String data = getPrivateKeyForCPA(randomMd5);
		int number = new Random().nextInt(10);
		StringBuffer sb = new StringBuffer();
		sb.append(randomMd5.substring(0,number)).append(data).append(randomMd5.substring(number));
		map.put(sb.toString(), number);
		return map;
	}
	
	
    /**
     * 获取CPA私钥(randomMd5+干扰字符串进行md5加密后截取16位的字符串)
     * @param randomMd5
     * @return
     * @throws Exception
     */
	public static String getPrivateKeyForCPA(String randomMd5) {
		String data = CryptUtil.md5(randomMd5 + CPA_Ad_md5Key).toLowerCase();
		data = data.substring(8, 24);
		return data;
	}
	
    /**
     * 根据randomMd5和privateKeyForCPA生成CPA的验证码与随机数的对应关系map
     * @param randomMd5
     * @param privateKeyForCPA
     * @return
     * @throws Exception
     */
	public static Map<String,Integer> getMd5Pass(String randomMd5,String privateKeyForCPA) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		int number = new Random().nextInt(10);
		StringBuffer sb = new StringBuffer();
		sb.append(randomMd5.substring(0,number)).append(privateKeyForCPA).append(randomMd5.substring(number));
		map.put(sb.toString(), number);
		return map;
	}
	
	public static void main(String[] args){		
		try {
			 String randomMd5 = getRandomMd5(16);
			 System.out.println("randomMd5=="+randomMd5);
			 String privateKey = getPrivateKeyForCPA(randomMd5);
			 System.out.println("privateKey=="+privateKey);
			 Map<String,Integer>  md5PassMap = getMd5Pass(randomMd5,privateKey);
			 for (Map.Entry<String, Integer> entry : md5PassMap.entrySet()) {
				 System.out.println("key=="+entry.getKey()+" value=="+entry.getValue());
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
}
